﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WLWeb.UC
{
    public partial class PageSize : System.Web.UI.UserControl
    {
        #region 属性
        protected string classStyle = "fanyi left width630";
        /// <summary>
        /// 样式
        /// </summary>
        public string ClassStyle
        {
            get { return classStyle; }
            set { classStyle = value; }
        }

        /// <summary>
        /// 跳转的页面
        /// </summary>
        public string PageUrl
        {
            get
            {
                if (ViewState["PageUrl"] != null)
                {
                    return ViewState["PageUrl"].ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                ViewState["PageUrl"] = value;
            }
        }
        private int pageCount;
        /// <summary>
        /// 页面总页数(可传可不传)
        /// </summary>
        public int PageCount
        {
            get { return pageCount; }
            set { pageCount = value; }
        }

        /// <summary>
        /// 当前页数
        /// </summary>
        public int PageIndex
        {

            get
            {
                if (ViewState["pageIndex"] != null)
                {
                    return Convert.ToInt32(ViewState["pageIndex"].ToString());
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                ViewState["pageIndex"] = value;
            }
        }
        private int pageSizeNum = 20;
        /// <summary>
        /// 每页显示的条数
        /// </summary>
        public int PageSizeNum
        {
            get { return pageSizeNum; }
            set { pageSizeNum = value; }
        }
        private int dataCount;
        /// <summary>
        /// 数据源的总记录数
        /// </summary>
        public int DataCount
        {
            get { return dataCount; }
            set { dataCount = value; }
        }

        private string pageFlag = "page";
        public string PageFlag
        {
            get { return pageFlag; }
            set { pageFlag = value; }
        }

        private string pagelable = "1";
        public string Pagelable
        {
            get { return pagelable; }
            set { pagelable = value; }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ViewState["pageIndex"] == null)
                {
                    ViewState["pageIndex"] = 0;
                }

                if (Pagelable == "1")
                {
                    hlPre.Text = "上一页";
                    hlNext.Text = "下一页";
                }
                if (Pagelable == "2")
                {
                    hlPre.Text = "<";
                    hlNext.Text = ">";
                }
            }
        }
        public void ControlInit()
        {
            BindPage();
        }

        private void BindPage()
        {
            // lbSize.Text = pageSizeNum.ToString();
            if (!string.IsNullOrEmpty(PageUrl))
            {
                if (!PageUrl.Contains("?"))
                {
                    PageUrl += "?"; //页面后无参数的处理方式
                }
            }

            #region 处理分页标签
            //  lbRecordNum.Text = dataCount.ToString();
            pageCount = (((dataCount % pageSizeNum) == 0 ? dataCount / pageSizeNum : (dataCount / pageSizeNum) + 1));
            //   lbPageNum.Text = pageCount.ToString();
            hlPre.NavigateUrl = PageUrl + pageFlag + "=" + (PageIndex == 0 ? "0" : (PageIndex - 1).ToString());
            if (PageIndex == 0)
            {
                hlPre.Enabled = false;
            }
            hlNext.NavigateUrl = PageUrl + pageFlag + "=" +
                                 (PageIndex == pageCount ? PageIndex.ToString() : (PageIndex + 1).ToString());
            if ((PageIndex == (pageCount - 1)) || (pageCount == 0))
            {
                hlNext.Enabled = false;
            }

            if (pageCount < 5)
            {
                //标签一
                hlOne.NavigateUrl = PageUrl + pageFlag + "=0";
                hlOne.Text = "1";
                hlTwo.Visible = pageCount >= 1 ? true : false;
                //标签二
                hlTwo.NavigateUrl = PageUrl + pageFlag + "=1";
                hlTwo.Text = "2";
                hlTwo.Visible = pageCount >= 2 ? true : false;
                //标签三
                hlThree.NavigateUrl = PageUrl + pageFlag + "=2";
                hlThree.Text = "3";
                hlThree.Visible = pageCount >= 3 ? true : false;
                //标签四
                hlFour.NavigateUrl = PageUrl + pageFlag + "=3";
                hlFour.Text = "4";
                hlFour.Visible = pageCount >= 4 ? true : false;
                //标签五
                hlFive.NavigateUrl = PageUrl + pageFlag + "=4";
                hlFive.Text = "5";
                hlFive.Visible = pageCount >= 5 ? true : false;
            }
            else
            {
                if (PageIndex > 2)
                {
                    //标签一
                    hlOne.NavigateUrl = PageUrl + pageFlag + "=" + (PageIndex - 2);
                    hlOne.Text = (PageIndex - 1).ToString(); //"1";
                    hlTwo.Visible = pageCount > (PageIndex - 2) ? true : false;
                    //标签二
                    hlTwo.NavigateUrl = PageUrl + pageFlag + "=" + (PageIndex - 1);
                    hlTwo.Text = PageIndex.ToString(); //"2";
                    hlTwo.Visible = pageCount > (PageIndex - 1) ? true : false;
                    //标签三
                    hlThree.NavigateUrl = PageUrl + pageFlag + "=" + (PageIndex);
                    hlThree.Text = (PageIndex + 1).ToString();
                    hlThree.Visible = pageCount > PageIndex ? true : false;
                    //标签四
                    hlFour.NavigateUrl = PageUrl + pageFlag + "=" + (PageIndex + 1);
                    hlFour.Text = (PageIndex + 2).ToString();
                    hlFour.Visible = pageCount > (PageIndex + 1) ? true : false;
                    //标签五
                    hlFive.NavigateUrl = PageUrl + pageFlag + "=" + (PageIndex + 2);
                    hlFive.Text = (PageIndex + 3).ToString();
                    hlFive.Visible = pageCount > (PageIndex + 2) ? true : false;
                }
                else
                {

                    //标签一
                    hlOne.NavigateUrl = PageUrl + pageFlag + "=0";
                    hlOne.Text = "1";
                    hlTwo.Visible = pageCount >= 1 ? true : false;
                    //标签二
                    hlTwo.NavigateUrl = PageUrl + pageFlag + "=1";
                    hlTwo.Text = "2";
                    hlTwo.Visible = pageCount >= 2 ? true : false;
                    //标签三
                    hlThree.NavigateUrl = PageUrl + pageFlag + "=2";
                    hlThree.Text = "3";
                    hlThree.Visible = pageCount >= 3 ? true : false;
                    //标签四
                    hlFour.NavigateUrl = PageUrl + pageFlag + "=3";
                    hlFour.Text = "4";
                    hlFour.Visible = pageCount >= 4 ? true : false;
                    //标签五
                    hlFive.NavigateUrl = PageUrl + pageFlag + "=4";
                    hlFive.Text = "5";
                    hlFive.Visible = pageCount >= 5 ? true : false;
                }

                if (PageIndex > 4)
                {

                    hlStar.NavigateUrl = PageUrl + pageFlag + "=0";
                    hlStar.Text = "1...";
                    hlStar.Visible = true;
                }
                else
                {

                    hlEnd.NavigateUrl = PageUrl + pageFlag + "=" + (pageCount - 1);
                    hlEnd.Text = "..." + (pageCount).ToString();
                    hlEnd.Visible = true;
                }
            }
            //else if ((pageCount - PageIndex) <= 5)
            //{
            //    int intTemp;
            //    intTemp = PageIndex - (5 - (pageCount - PageIndex));
            //    //标签一
            //    hlOne.Text = (intTemp + 1).ToString();
            //    hlOne.NavigateUrl = PageUrl + pageFlag + "=" + (intTemp);
            //    //标签二
            //    hlTwo.Text = (intTemp + 2).ToString();
            //    hlTwo.NavigateUrl = PageUrl + pageFlag + "=" + (intTemp + 1);
            //    //标签三
            //    hlThree.Text = (intTemp + 3).ToString();
            //    hlThree.NavigateUrl = PageUrl + pageFlag + "=" + (intTemp + 2);
            //    //标签四
            //    hlFour.Text = (intTemp + 4).ToString();
            //    hlFour.NavigateUrl = PageUrl + pageFlag + "=" + (intTemp + 3);
            //    //标签五
            //    hlFive.Text = (intTemp + 5).ToString();
            //    hlFive.NavigateUrl = PageUrl + pageFlag + "=" + (intTemp + 4);
            //}
            //else if ((pageCount - PageIndex) > 5)
            //{
            //    //标签一
            //    hlOne.NavigateUrl = PageUrl + pageFlag + "=" + PageIndex;
            //    hlOne.Text = (PageIndex + 1).ToString();
            //    //标签二
            //    hlTwo.NavigateUrl = PageUrl + pageFlag + "=" + (PageIndex + 1);
            //    hlTwo.Text = (PageIndex + 2).ToString();
            //    //标签三
            //    hlThree.NavigateUrl = PageUrl + pageFlag + "=" + (PageIndex + 2);
            //    hlThree.Text = (PageIndex + 3).ToString();
            //    //标签四
            //    hlFour.NavigateUrl = PageUrl + pageFlag + "=" + (PageIndex + 3);
            //    hlFour.Text = (PageIndex + 4).ToString();
            //    //标签五
            //    hlFive.NavigateUrl = PageUrl + pageFlag + "=" + (PageIndex + 4);
            //    hlFive.Text = (PageIndex + 5).ToString();
            //}
            SetPageStyle();//设置标签的样式
            #endregion


        }



        private void SetPageStyle()
        {
            hlOne.CssClass = "color_zc";
            hlTwo.CssClass = "color_zc";
            hlThree.CssClass = "color_zc";
            hlFour.CssClass = "color_zc";
            hlFive.CssClass = "color_zc";
            PageIndex += 1;
            if (hlOne.Text.Trim() == PageIndex.ToString())
            {

                hlOne.CssClass = "black";
            }
            else if (hlTwo.Text.Trim() == PageIndex.ToString())
            {
                hlTwo.CssClass = "black";
            }
            else if (hlThree.Text.Trim() == PageIndex.ToString())
            {
                hlThree.CssClass = "black";
            }
            else if (hlFour.Text.Trim() == PageIndex.ToString())
            {
                hlFour.CssClass = "black";
            }
            else if (hlFive.Text.Trim() == PageIndex.ToString())
            {
                hlFive.CssClass = "black";
            }
            else
            {
                hlOne.CssClass = "black";
            }
        }

        public int GetCurrentPage()
        {
            int CurPage = 0;
            if (Request.QueryString["page"] != null && !string.IsNullOrEmpty(Request.QueryString["page"].ToString()))
            {
                CurPage = Convert.ToInt32(Request.QueryString["Page"].ToString());
            }
            else
            {
                CurPage = 0;
            }
            return CurPage;
        }

        public void ControlInit(int recordCount, int pageSizeNum, String url)
        {
            if (recordCount == 0)
            {
                this.Visible = false;
                return;
            }

            this.PageUrl = url; // "../Common/Dite.aspx";
            this.PageCount = recordCount / pageSizeNum;
            this.PageIndex = GetCurrentPage();
            this.PageSizeNum = pageSizeNum;
            this.DataCount = recordCount;
            ControlInit();
        }
    }
}